package com.chencs.app.product.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.app.AppCommon;
import com.chencs.app.product.service.AppProductService;
import com.chencs.biz.product.model.JuHuaSuanResponse;
import com.chencs.biz.product.model.SimilarProductResponse;
import com.chencs.biz.product.model.SimpleProductResponse;
import com.chencs.biz.product.model.TbkCouponResponse;
import com.chencs.utils.ApplyCommissionUtils;
import com.chencs.utils.GetTmailUrl;
import com.chencs.vo.biz.password.ApplyCommission;
import com.chencs.vo.net.request.*;
import com.chencs.vo.net.response.ApplyCommissionCouponsResponse;
import com.chencs.vo.net.response.JHSModelResponse;
import com.chencs.vo.net.response.ProductListResponse;
import com.chencs.vo.net.response.ResponseBase;
import com.taobao.api.response.TbkItemRecommendGetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created by fangzehua on 2017/7/11.
 */
@RestController
@RequestMapping("/app/product")
public class AppProductController {


    @Autowired
    private AppProductService appProductService;

    /**getProduct
     * 获取商品詳情
     * @return
     */
    @RequestMapping(value = "/findProduct", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getSimpleProduct(@RequestBody ProductRequest productRequest){
        SimpleProductResponse response = null;
        try {
            response = appProductService.getSimpleProductByPid(productRequest.getPid());
            if(response!=null){
                response.setCode(AppCommon.SUCCESS);
            }else{
                response = new SimpleProductResponse();
                response.setCode(AppCommon.NULL);
            }
        }catch (Exception e){
            e.printStackTrace();
            response = new SimpleProductResponse();
            response.setCode(AppCommon.ERROR);
        }
     //   System.out.println(JSON.toJSONString(response));
        return  JSON.toJSONString(response);
    }

    @RequestMapping(value = "/getProductList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getProductList(@RequestBody ProductListRequest  ProductListRequest){
        ProductListResponse response = null;
        try {
            response = appProductService.searchProducts(ProductListRequest);
            if(response!=null){
                response.setCode(AppCommon.SUCCESS);
            }else{
                response.setCode(AppCommon.NULL);
            }
        }catch (Exception e){
            response = new ProductListResponse();
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }

    /**
     * 相似商品
     * @param productRequest
     * @return
     */
    @RequestMapping(value = "/getSimilarProductList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getSimilarProductList(@RequestBody ProductRequest  productRequest){
        SimilarProductResponse response = null;
        try {
            response = appProductService.searchSimilarProducts(Long.parseLong(productRequest.getPid()));
            if(response==null){
                response.setCode(AppCommon.NULL);
            }else {
                response.setCode(AppCommon.SUCCESS);
            }
        }catch (Exception e){
          e.printStackTrace();
            response = new SimilarProductResponse();
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }


    /**
     * @param productRequest
     * @return
     */
    @RequestMapping(value = "/getParityProductList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getParityProductList(@RequestBody ProductRequest  productRequest){
        TbkCouponResponse response = null;
        try {
            response = appProductService.getParityProductList(productRequest);
            if(response==null){
                response = new TbkCouponResponse();
                response.setCode(AppCommon.NULL);
            }else {
                response.setCode(AppCommon.SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            response = new TbkCouponResponse();
            response.setCode(AppCommon.ERROR);
        }
       // System.out.println(JSON.toJSONString(response));
        return  JSON.toJSONString(response);
    }


    //top榜单
    /**
     * @return
     */
    @RequestMapping(value = "/topList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String topList(@RequestBody PushRequest pushRequest){
        ProductListResponse response =  new ProductListResponse();
        try {
            response = appProductService.topList(pushRequest);
            if(response==null){
                response =  new ProductListResponse();
                response.setCode(AppCommon.NULL);
            }else {
                response.setCode(AppCommon.SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }

    /**
     * @return
     */
    @RequestMapping(value = "/dayUpdate", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String dayUpdate(@RequestBody PushRequest  pushRequest){
        ProductListResponse response =  new ProductListResponse();;
        try {
            response = appProductService.dayUpdate(pushRequest);
            if(response==null){
                response =  new ProductListResponse();
                response.setCode(AppCommon.NULL);
            }else {
                response.setCode(AppCommon.SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }



    /**
     * @return
     */
    @RequestMapping(value = "/getJHSList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String dayUpdate(@RequestBody ProductRequest request){
        JuHuaSuanResponse response =  new JuHuaSuanResponse();;
        try {
            response = appProductService.juHuaSuan(request);
            if(response==null){
                response.setCode(AppCommon.NULL);
            }else {
                response.setCode(AppCommon.SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }


    /**
     * @return
     */
    @RequestMapping(value = "/getjhsItem", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getjhsItem(@RequestBody RequestBase request){
        JHSModelResponse response =  new JHSModelResponse();;
        try {
            response = appProductService.getJhsItem(request);
            if(response==null){
                response.setCode(AppCommon.NULL);
            }else {
                response.setCode(AppCommon.SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }


    /**
     * @return
     */
    @RequestMapping(value = "/getCommand5", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCommand5(@RequestBody ProductRequest productRequest) {
        SimpleProductResponse response = new SimpleProductResponse();
        if (true) {
            response = appProductService.getComd2(productRequest.getKey());
            if (response == null) {
                response = new SimpleProductResponse();
                response.setMsg("内部优惠券被抢完了~");
                response.setCode(AppCommon.NULL);
            } else {
                response.setCode(AppCommon.SUCCESS);
            }

        }
        return  JSON.toJSONString(response);
    }


    /**
     * @return
     */
    @RequestMapping(value = "/getCommand", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCommand(@RequestBody ProductRequest productRequest) {

            SimpleProductResponse response = appProductService.getComdXXXXX(productRequest.getKey());
            if (response == null) {
                response = new SimpleProductResponse();
                response.setMsg("晚了一步,内部优惠券被抢完了~");
                response.setCode(AppCommon.NULL);
                return JSON.toJSONString(response);
            } else {
                response.setCode(AppCommon.SUCCESS);
                System.out.println(JSON.toJSONString(response));
                return JSON.toJSONString(response);
            }

    }



//            if (response ==null){
//                try {
//                    String newCode = new String(decode(productRequest.getKey()));
//                  //  System.out.println(newCode);
//                    if (newCode.indexOf("淘口令")!=-1||newCode.indexOf("淘宝")!=-1||newCode.indexOf("手淘")!=-1){
//                        response = appProductService.getComd(newCode);
//                    }else if(newCode.indexOf("喵口令")!=-1){
//                        response =  appProductService.getTmallComd(GetTmailUrl.getTmallProductTitle(newCode));
//                    }else{
//                        response.setCode(AppCommon.NULL);
//                    }
//                    if(response==null){
//                        response =  new SimpleProductResponse();
//                        response.setCode(AppCommon.NULL);
//
//                    }else{
//                        response.setCode(AppCommon.SUCCESS);
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                    response.setCode(AppCommon.ERROR);
//                }
//            }



//            try {
//                String newCode = new String(decode(productRequest.getKey()));
//                System.out.println(newCode);
//                if (newCode.indexOf("淘口令")!=-1||newCode.indexOf("淘宝")!=-1||newCode.indexOf("手淘")!=-1){
//                    response = appProductService.getComd(newCode);
//                }else if(newCode.indexOf("喵口令")!=-1){
//                    response =  appProductService.getTmallComd(GetTmailUrl.getTmallProductTitle(newCode));
//                }else{
//                    response.setCode(AppCommon.NULL);
//                }
//                if(response==null){
//                    response =  new SimpleProductResponse();
//                    response.setCode(AppCommon.NULL);
//
//                }else{
//                    response.setCode(AppCommon.SUCCESS);
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//                response.setCode(AppCommon.ERROR);
//            }
      //      System.out.println(JSON.toJSONString(response));


 //       }
      // System.out.println(JSON.toJSONString(response));






    /**
     * @return
     */
    @RequestMapping(value = "/getCommand2", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCommand2(@RequestBody ProductRequest productRequest){
      //  System.out.println(JSON.toJSONString(productRequest));
        SimpleProductResponse response =  new SimpleProductResponse();
        appProductService.getQuan("");
      //  System.out.println(JSON.toJSONString(response));
        return  JSON.toJSONString(response);
    }

    /**
     * @return
     */
    @RequestMapping(value = "/getCommand3", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getCommand3(@RequestBody ProductRequest productRequest){
      //  System.out.println(JSON.toJSONString(productRequest));
        SimpleProductResponse response =  new SimpleProductResponse();
        appProductService.getXPDR("");
     //   System.out.println(JSON.toJSONString(response));
        return  JSON.toJSONString(response);
    }




    /**
     * 高佣金
     * @return
     */
    @RequestMapping(value = "/applyCommission", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String applyCommission(@RequestBody ApplyCommissionRequest productRequest){
       // System.out.println(JSON.toJSONString(productRequest));
        ResponseBase response =  new ResponseBase();
        if(productRequest.getUrl().indexOf("https://uland")==-1){
            ApplyCommission commission =  ApplyCommissionUtils.get("",productRequest.getMmid(),productRequest.getUrl());
            appProductService.applyCommission(JSON.toJSONString(productRequest),commission);
            response.setMsg("解析成功 succ----------");
        }else{
            response.setMsg("uland-------------");
        }

        return  JSON.toJSONString(response);
    }


    /**
     * 高佣金用PD
     * @return
     */
    @RequestMapping(value = "/applyCommissionByPid", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String applyCommissionByPid(@RequestBody ApplyCommissionRequest request){

        return  JSON.toJSONString(appProductService.applyCommissionByPid(request));
    }



    /**
     * 高佣金用 url
     * @return
     */
    @RequestMapping(value = "/applyCommissionForCoupons", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String applyCommissionForCounp(@RequestBody ApplyCommissionRequest request){
        ApplyCommissionCouponsResponse response = new ApplyCommissionCouponsResponse();
        if(request.getUrl().indexOf("https://uland")==-1){
            response = appProductService.applyCommissionForCoupons(request);
        }else{
            response.setCode(-1);
            response.setMsg("地址不对");
        }

        return  JSON.toJSONString(response);
    }


    public static byte[] decode(String str){
        byte[] bt = null;
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            bt = decoder.decodeBuffer( str );
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bt;
    }



}
